padVector <- function(vector,desiredLength){
  if(length(vector) < desiredLength){
    paddingLength = desiredLength - length(vector)
    return(append(vector,rep(vector[length(vector)],paddingLength)))
  }
  return(vector)
}

extractMedianError <- function(inputFile){
  #Read data and compute errors for each cluster
  inputData <- read.table(inputFile,header=TRUE)
  return(inputData[,1])
}

extractMeanError <- function(inputFile){
  #Read data and compute errors for each cluster
  inputData <- read.table(inputFile,header=TRUE)
  return(inputData[,2])
}

extractMedianWorking <- function(inputFile){
  #Read data and compute errors for each cluster
  inputData <- read.table(inputFile,header=TRUE)
  return(inputData[,3])
}

extractMeanWorking <- function(inputFile){
  #Read data and compute errors for each cluster
  inputData <- read.table(inputFile,header=TRUE)
  return(inputData[,4])
}

#############################################################################
library(MASS)

# $1 - Directory where the .occ files corresponding to the different experiments are stored
# $2 - Experiment duration (seconds)
args <- commandArgs(trailingOnly = TRUE)
workingDirectory <- args[1]
rm(args)

#Set the directory containing the experiment results as current working directory
setwd(paste("/home/deste/argos2/user/jdestefani/results/Tests/",workingDirectory,"/",sep=""))
#List all the files containing information about occupation
resultsFiles <- list.files(pattern = "\\.samples$")
print(resultsFiles)


print("[STATUS] - Processing results file")
#Process every single file separately
medianError <- lapply(resultsFiles,extractMedianError)
medianWorking <- lapply(resultsFiles,extractMedianWorking)
meanError <- lapply(resultsFiles,extractMeanError)
meanWorking <- lapply(resultsFiles,extractMeanWorking)

# Statistical tests
#Compare best vs. ﬁrst-improvement for each neighborhood
NPMedianErrorWilcoxon <- wilcox.test(medianError[[3]],medianError[[1]],paired=TRUE)
NIMedianErrorWilcoxon <- wilcox.test(medianError[[3]],medianError[[2]],paired=TRUE)
PIMedianErrorWilcoxon <- wilcox.test(medianError[[1]],medianError[[2]],paired=TRUE)
NPMedianWorkingWilcoxon <- wilcox.test(medianWorking[[3]],medianWorking[[1]],paired=TRUE)
NIMedianWorkingWilcoxon <- wilcox.test(medianWorking[[3]],medianWorking[[2]],paired=TRUE)
PIMedianWorkingWilcoxon <- wilcox.test(medianWorking[[1]],medianWorking[[2]],paired=TRUE)

NPMeanErrorWilcoxon <- wilcox.test(meanError[[3]],meanError[[1]],paired=TRUE)
NIMeanErrorWilcoxon <- wilcox.test(meanError[[3]],meanError[[2]],paired=TRUE)
PIMeanErrorWilcoxon <- wilcox.test(meanError[[1]],meanError[[2]],paired=TRUE)
NPMeanWorkingWilcoxon <- wilcox.test(meanWorking[[3]],meanWorking[[1]],paired=TRUE)
NIMeanWorkingWilcoxon <- wilcox.test(meanWorking[[3]],meanWorking[[2]],paired=TRUE)
PIMeanWorkingWilcoxon <- wilcox.test(meanWorking[[1]],meanWorking[[2]],paired=TRUE)


print("[STATUS] - Producing Files")
write(paste("Naive vs Probabilistic - Error (Median)",NPMedianErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Naive vs Informed - Error (Median)",NIMedianErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Probabilistic vs Informed - Error (Median)",PIMedianErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write("\n",paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Naive vs Probabilistic - Working (Median)",NPMedianWorkingWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Naive vs Informed - Working (Median)",NIMedianWorkingWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Probabilistic vs Informed - Working (Median)",PIMedianErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write("\n",paste(workingDirectory,"tests",sep="."),append=TRUE)        
write(paste("Naive vs Probabilistic - Error (Mean)",NPMeanErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Naive vs Informed - Error (Mean)",NIMeanErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Probabilistic vs Informed - Error (Mean)",PIMeanErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write("\n",paste(workingDirectory,"tests",sep="."),append=TRUE)          
write(paste("Naive vs Probabilistic - Working (Mean)",NPMeanErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Naive vs Informed - Working (Mean)",NIMeanErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)
write(paste("Probabilistic vs Informed - Working (Mean)",PIMeanErrorWilcoxon$p.value,sep="\t"),paste(workingDirectory,"tests",sep="."),append=TRUE)


rm(resultsFiles)
